package com.jy.adscm.system.controller;

import com.jy.adscm.annotation.AdscmLogger;
import com.jy.adscm.bean.system.JyRole;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.support.SimpleResponse;
import com.jy.adscm.system.service.JyRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yyy
 * @ClassName AdscmBaseController
 * @Description 角色Controller
 * @date 2018/8/2 17:40
 */
@RestController
@CrossOrigin
@RequestMapping("/adscm/system/role")
public class AdscmRoleController {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    JyRoleService jyRoleService;

    /**
     * 获取所有角色
     * @return
     */
    @AdscmLogger("获取所有角色")
    @RequestMapping("/getAllRoles")
    public SimpleResponse getAllRoles()throws AdscmSystemException {

        logger.info("获取所有角色");

        List<JyRole> roles = jyRoleService.selectList(null);

        return new SimpleResponse("200","获取所有角色成功",roles);

    }
    /**
     * 删除角色
     * @return
     */
    @AdscmLogger("删除角色")
    @RequestMapping("/delRole/{roleId}")
    public SimpleResponse delRole(@PathVariable int roleId)throws AdscmSystemException{

        jyRoleService.delRole(roleId);

        return new SimpleResponse("200","删除角色【"+roleId+"】成功",null);

    }
    /**
     * 增加角色
     * @return
     */
    @AdscmLogger("增加角色")
    @RequestMapping("/addRole")
    public SimpleResponse addRole( @RequestBody JyRole role)throws AdscmSystemException{

        jyRoleService.insert(role);

        return new SimpleResponse("200","增加角色成功",role);

    }
    /**
     * 给角色关联菜单
     * @return
     */
    @AdscmLogger("给角色关联菜单")
    @RequestMapping("/rolesMenus/{roleId}")
    public SimpleResponse rolesMenus(@PathVariable("roleId") int roleId,String menuIds)throws AdscmSystemException{


        List<Integer> ids = new ArrayList<>();
        if(menuIds != null &&!"".equals(menuIds)){
            String[] ids_str = menuIds.split(",");
            for (String s : ids_str) {
                ids.add(Integer.parseInt(s));
            }
        }

        jyRoleService.setRoleMenus(roleId,ids);

        return new SimpleResponse("200","给角色关联菜单成功",null);

    }

}
